package com.cn.jx.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cn.jx.common.controller.BaseController;
import com.cn.jx.common.util.jpaQuery.ConvertCondition;
import com.cn.jx.common.util.jpaQuery.PageCondition;
import com.cn.jx.common.util.jpaQuery.QueryCondition;
import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.common.vo.SessionUser;
import com.cn.jx.manager.form.NoticeForm;
import com.cn.jx.manager.po.Notice;
import com.cn.jx.manager.service.mtn.IMessageServiceMtn;
import com.cn.jx.manager.service.mtn.INoticeServiceMtn;
import com.cn.jx.manager.service.query.IMessageServiceQry;
import com.cn.jx.manager.service.query.INoticeServiceQry;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.util.FormValidat;
import com.jx.util.MapObject;
import com.jx.util.Response;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月5日
 * <p>功能描述：文件管理
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Controller
@RequestMapping("/admin/office/")
public class OfficeController extends BaseController{

	@Autowired
	private INoticeServiceQry noticeServiceQry;
	
	@Autowired
	private INoticeServiceMtn noticeServiceMtn;
	
	@Autowired
	private IMessageServiceQry messageServiceQry;
	
	@Autowired
	private IMessageServiceMtn messageServiceMtn;
	
	/**
	 * 
	* @Title:noticeList
	* @Description:TODO(通知列表)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("notice/list/{path}")
	public String noticeList(Model model, HttpServletRequest request,@PathVariable(value = "path") String path) {
		SessionUser sessUser = getSessionUser(request);
		setModelColumns(model, DataGrideTypeEnum.NOTICE_MGR);
		setModelPermit(model,ResourceEnum.NOTICE,sessUser.getResourceCodeList());
		return "recycle".equals(path)?"message/notice/noticerecycle":"message/noticelist";
	}
	/**
	 * 
	* @Title:add
	* @Description:TODO(跳转到熬添加)
	* <p>邮箱地址: 524577360@qq.com
	* String
	* @throws
	 */
	@RequestMapping("notice/add")
	public String add(Model model, HttpServletRequest request, String id) {
		// id存在 则为修改编辑
		Notice re = null;
		if (Utility.isNotBlank(id)) {
			re = noticeServiceQry.getNoticeById(id);
		}
		model.addAttribute("res", re);
		return "message/notice/noticeedit";
	}
	
	@RequestMapping("notice/get")
	public String getNoticeById(Model model, HttpServletRequest request, String id) {
		Notice uf = noticeServiceQry.getNoticeById(id);
		model.addAttribute("res", uf);
		return "message/notice/noticeDetail";
	}
	
	/**
	 * 
	* @Title:noticeList
	* @Description:TODO(这里用一句话描述这个方法的作用)
	* <p>邮箱地址: 524577360@qq.com
	* PageObj<MapObject>
	* @throws
	 */
	@RequestMapping("notice/listData/{path}")
	@ResponseBody
	public PageObj<MapObject> noticeList(Model model,@PathVariable(value = "path") String path, PageCondition page) {
		QueryCondition cond = ConvertCondition.initCondition(page, path);
		try {
			PageObj<MapObject> pageuser = noticeServiceQry.findNoticePage(cond);
			return pageuser;
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	* @Title:addNotice
	* @Description:TODO(这里用一句话描述这个方法的作用)
	* <p>邮箱地址: 524577360@qq.com
	* Response
	* @throws
	 */
	@RequestMapping("notice/addOrUpdate")
	@ResponseBody
	public Response addNotice(Model model, HttpServletRequest request, NoticeForm noticeForm) {
		SessionUser sessUser = getSessionUser(request);
		// 表单验证
		try {
			FormValidat.exec(noticeForm);
			noticeForm.setOperatUser(sessUser.getUserId());
			noticeServiceMtn.saveOrUpdateNotice(noticeForm);
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	 @RequestMapping("notice/delete")
	 @ResponseBody
	 public Response delNotice(Model model,HttpServletRequest request,String ids) {
		SessionUser sessUser= getSessionUser(request);
		 try {
			 FormValidat.validId(ids);
			 //表单验证
			 List<String> list=JSON.parseArray(ids, String.class);
			 noticeServiceMtn.del(list,sessUser.getUserId());
			 return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	 }
	 
	 /**
		 * 
		* @Title:removeRole
		* @Description:TODO(移除角色 清空回收站)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
		@RequestMapping("notice/remove")
		@ResponseBody
		public Response removeNotice(Model model, HttpServletRequest request, String ids) {
			SessionUser sessUser = getSessionUser(request);
			try {
				// 表单验证
				FormValidat.validId(ids);
				// 表单验证
				List<String> list = JSON.parseArray(ids, String.class);
				noticeServiceMtn.remove(list, sessUser.getUserId());
				return Response.Success();
			} catch (Exception e) {
				log.debug(e.getMessage());
				return Response.Fail(e.getMessage());
			}
		}
		
		/**
		 * 
		* @Title:removeRole
		* @Description:TODO(找回)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
		@RequestMapping("notice/recover")
		@ResponseBody
		public Response recoverNotice(Model model, HttpServletRequest request, String ids) {
			SessionUser sessUser = getSessionUser(request);
			try {
				// 表单验证
				FormValidat.validId(ids);
				// 表单验证
				List<String> list = JSON.parseArray(ids, String.class);
				noticeServiceMtn.recover(list, sessUser.getUserId());
				return Response.Success();
			} catch (Exception e) {
				log.debug(e.getMessage());
				return Response.Fail(e.getMessage());
			}
		}
		
		/**
		 * 
		* @Title:removeRole
		* @Description:TODO(移除角色 清空回收站)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
		@RequestMapping("notice/removeAll")
		@ResponseBody
		public Response removeAllNotice(Model model, HttpServletRequest request) {
			SessionUser sessUser = getSessionUser(request);
			try {
				noticeServiceMtn.removeAll(sessUser.getUserId());
				return Response.Success();
			} catch (Exception e) {
				log.debug(e.getMessage());
				return Response.Fail(e.getMessage());
			}
		}
	
	
//*************************************************上面是Notice************************************************************************************************	
	 
	 /**
		 * 
		* @Title:noticeList
		* @Description:TODO(消息列表)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
	 @RequestMapping("message/list")
	 public String messageList(Model model, HttpServletRequest request) {
		 SessionUser sessUser = getSessionUser(request);
		 
		 model.addAttribute("noReadNum",  messageServiceQry.findInboxNoReadCount(sessUser.getUserId()));
		 model.addAttribute("totalNum", messageServiceQry.findInboxTotalCount(sessUser.getUserId()));
		 model.addAttribute("unSendNum", messageServiceQry.findOutboxUnSendCount(sessUser.getUserId()));
		 model.addAttribute("sendNum", messageServiceQry.findOutboxSendCount(sessUser.getUserId()));
		 model.addAttribute("delNum", messageServiceQry.findDeleteCount(sessUser.getUserId()));
		 
		 setModelColumns(model, DataGrideTypeEnum.MESSAGE_MGR);
		 
		 return "message/emaillist";
	 }
	 
	 
	 @RequestMapping("message/detail")
	 public String messageDetail(Model model, HttpServletRequest request,String id) {
		 SessionUser sessUser = getSessionUser(request);
		 model.addAttribute("noReadNum",  messageServiceQry.findInboxNoReadCount(sessUser.getUserId()));
		 model.addAttribute("totalNum", messageServiceQry.findInboxTotalCount(sessUser.getUserId()));
		 model.addAttribute("unSendNum", messageServiceQry.findOutboxUnSendCount(sessUser.getUserId()));
		 model.addAttribute("sendNum", messageServiceQry.findOutboxSendCount(sessUser.getUserId()));
		 model.addAttribute("delNum", messageServiceQry.findDeleteCount(sessUser.getUserId()));
		
		 //更新id为已读
		 messageServiceMtn.readMessage(sessUser.getUserId(), id);
		 //获取id
		 model.addAttribute("messageDetail",  messageServiceQry.findMessage(id));
		 
		 return "message/email/emaildetail";
	 }
	 
	 /**
	  * 
	 * @Title:messageList
	 * @Description:TODO(分页查询)
	 * <p>邮箱地址: 524577360@qq.com
	 * @param model
	 * @param page
	 * @return
	 * PageObj<MapObject>
	 * @throws
	  */
	 @RequestMapping("message/listData")
		@ResponseBody
		public PageObj<MapObject> messageList(Model model, HttpServletRequest request, PageCondition page) {
		 SessionUser sessUser = getSessionUser(request);
			try {
				PageObj<MapObject> pageuser = messageServiceQry.findEmailMessage(page,sessUser.getUserId());
				return pageuser;
			} catch (Exception e) {
				log.debug(e.getMessage());
				e.printStackTrace();
			}
			return null;
		}
	 
	 /**
		 * 
		* @Title:noticeList
		* @Description:TODO(任务列表)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
	 @RequestMapping("task/list")
	 public String taskList(Model model) {
		 
		 return "document/dictionaryDesign";
	 }
	 
	 /**
		 * 
		* @Title:noticeList
		* @Description:TODO(流程列表)
		* <p>邮箱地址: 524577360@qq.com
		* @throws
		 */
	 @RequestMapping("process/list")
	 public String processList(Model model) {
		 
		 return "document/dictionaryDesign";
	 }
}
